<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_inputtextareaeditor} function plugin
 *
 * File:       function.pp_inputtextareaeditor.php<br>
 * Type:       function<br>
 * Name:       pp_inputtextareaeditor<br>
 * Date:       24.Feb.2003<br>
 * Purpose:    Prints out a list of text input types<br>
 * Input:<br>
 *           - name       (optional) - string default "text"
 *           - values     (required) - array
 *           - options    (optional) - associative array
 *           - checked    (optional) - array default not set
 *           - separator  (optional) - ie <br> or &nbsp;
 *           - output     (optional) - the output next to each text button
 *           - assign     (optional) - assign the output as an array to this variable
 * Examples:
 * <pre>
 * {pp_inputtextareaeditor values=$ids output=$names}
 * {pp_inputtextareaeditor values=$ids name='box' separator='<br>' output=$names}
 * {pp_inputtextareaeditor values=$ids checked=$checked separator='<br>' output=$names}
 * </pre>
 * @link http://smarty.php.net/manual/en/language.function.html.texts.php {pp_inputtextareaeditor}
 *      (Smarty online manual)
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
 * @author credits to Monte Ohrt <monte at ohrt dot com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_inputtextareaeditor($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
	
	$id = NULL;
	$name = NULL;
	$value = NULL;
	$text = NULL;
	$editor = NULL;
	$mode = NULL;
	$extra = NULL;
	
	$require = NULL;
	$requirement = NULL;
	
	foreach($params as $_key => $_val) {
		switch($_key) {
			case 'id':
				$$_key = (string)$_val;
				break;
				
			case 'name':
				$$_key = (string)$_val;
				break;
			
			case 'value':
				$$_key = (string)$_val;
				break;
			
			case 'text':
				$$_key = (string)$_val;
				break;
			
			case 'require':
				$$_key = (string)$_val;
				break;
				
			case 'requirement':
				$$_key = (string)$_val;
				break;
				
			case 'editor':
				$$_key = (string)$_val;
				break;
				
			case 'mode':
				$$_key = (string)$_val;
				break;

			case 'assign':
				break;
			
			default:
				if( !is_array($_val) ) {
					$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
				} else {
					$smarty->trigger_error("pp_inputtextareaeditor: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
				}
				break;
		}
	}

	//
	$_html_result = smarty_function_pp_inputtextareaeditor_output($id, $name, $value, $require, $requirement, $editor, $mode, $extra);

	return $_html_result;
}

function smarty_function_pp_inputtextareaeditor_output($id, $name, $value, $require, $requirement, $editor, $mode, $extra) {
	$_output = '';
	
	//if( isset($_REQUEST[$name]) ) {
	//	$value = (string)$_REQUEST[$name];
	//}
	if( strstr($name, '[]') === false ) {
		if( isset($_REQUEST[$name]) ) {
			$value = (string)$_REQUEST[$name];
		}
	} else {
		$nameb = str_replace('[]', '', $name);
		if( !isset($_REQUEST[$nameb . '_index']) ) {
			$_REQUEST[$nameb . '_index'] = 0;
		}
		if( isset($_REQUEST[$nameb][$_REQUEST[$nameb . '_index']]) ) {
			$value = (string)$_REQUEST[$nameb][$_REQUEST[$nameb . '_index']];
		}
		$_REQUEST[$nameb . '_index']++;
	}
	
	if( $editor == 'fckeditor' ) {
		//
	} else {//TINYMCE
		$js_file = CONFIG_URL_HTTP . CONFIG_DIR_EDITOR . 'tinymce/jscripts/tiny_mce/tiny_mce.js';
		//
		if( isset($_REQUEST['editor_editorid']) && $_REQUEST['editor_editorid'] > 0 ) {
			$_REQUEST['editor_editorid']++;
		} else {
			$_REQUEST['editor_editorid'] = 1;
			$_output .='<script language="javascript" type="text/javascript" src="' . $js_file . '"></script>';
		}
		//
		if( $mode == 'advanceda' ) {
			$editor_content = '
<!-- tinyMCE -->
<script language="javascript" type="text/javascript">
	tinyMCE.init({
		mode : "exact",
		elements : "' . $id . '",
		theme : "advanced",
		plugins : "safari,spellchecker,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,pagebreak,imagemanager,filemanager",
		theme_advanced_buttons1_add_before : "save,newdocument,separator",
		theme_advanced_buttons1_add : "fontselect,fontsizeselect",
		theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,separator,forecolor,backcolor",
		theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons3_add : "emotions,iespell,media,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,spellchecker,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		content_css : "/example_data/example_full.css",
		plugin_insertdate_dateFormat : "%Y-%m-%d",
		plugin_insertdate_timeFormat : "%H:%M:%S",
		external_link_list_url : "example_data/example_link_list.js",
		external_image_list_url : "example_data/example_image_list.js",
		flash_external_list_url : "example_data/example_flash_list.js",
		template_external_list_url : "example_data/example_template_list.js",
		theme_advanced_resize_horizontal : false,
		theme_advanced_resizing : true,
		apply_source_formatting : true,
		spellchecker_languages : "+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv"
	});
</script>
<!-- /tinyMCE -->';
		} elseif( $mode == 'advanced' ) {
		$editor_content = '
<!-- tinyMCE -->
<script language="javascript" type="text/javascript">
	tinyMCE.init({
		//mode : "textareas",
		//mode : "specific_textareas",
		mode : "exact",
		elements : "' . $id . '",
		theme : "advanced",
		plugins : "table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,zoom,flash,searchreplace,print,paste,directionality,fullscreen,noneditable,contextmenu",
		theme_advanced_buttons1_add_before : "save,newdocument,separator",
		theme_advanced_buttons1_add : "fontselect,fontsizeselect",
		theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor,liststyle",
		theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons3_add : "emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		content_css : "' . CONFIG_URL_HTTP . 'styles/' . CONFIG_STYLE . '/css/editor.css",
		plugin_insertdate_dateFormat : "%Y-%m-%d",
		plugin_insertdate_timeFormat : "%H:%M:%S",
		extended_valid_elements : "hr[class|width|size|noshade]",
		external_link_list_url : "example_link_list.js",
		external_image_list_url : "example_image_list.js",
		flash_external_list_url : "example_flash_list.js",
		file_browser_callback : "fileBrowserCallBack",
		paste_use_dialog : false,
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		theme_advanced_link_targets : "_something=My somthing;_something2=My somthing2;_something3=My somthing3;"
	});

	function fileBrowserCallBack(field_name, url, type, win) {
		// This is where you insert your custom filebrowser logic
		alert("Example of filebrowser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);

		// Insert new URL, this would normaly be done in a popup
		win.document.forms[0].elements[field_name].value = "someurl.htm";
	}
</script>
<!-- /tinyMCE -->';
		} elseif( $mode == 'simple' ) {
			$editor_content = '
<!-- tinyMCE -->
<script language="javascript" type="text/javascript">
	// Notice: The simple theme does not use all options some of them are limited to the advanced theme
	tinyMCE.init({
		mode : "exact",
		elements : "' . $id . '",
		theme : "simple",
	});
</script>
<!-- /tinyMCE -->';
		}
		//
		$_output .= $editor_content;
	}
	
	$_output .= '<textarea name="' . smarty_function_escape_special_chars($name) . '" id="' . $id . '" editorid="' . ($_REQUEST['editor_editorid'] - 1) . '" ';
	$_output .= $extra . '>';
	$value = str_replace(array('\r\n', '\n', '\r'), array("\r\n", "\n", "\r"), $value);
	$_output .= smarty_function_escape_special_chars(stripslashes($value));
	$_output .= '</textarea>';
	if( $require == true ) {
		if( $_REQUEST['_SUBMITTED_'] == 1 ) {
			if( isset($_REQUEST[$name]) && $_REQUEST[$name] == "" ) {
				if( $requirement ) {
					$_output .= '<span class="inputRequirement">' . $requirement . '</span>';
				} else {
					$_output .= '<span class="inputRequirement">*</span>';
				}
			} elseif( isset($_REQUEST[$name]) && $_REQUEST[$name] != "" ) {
				//
			}
		} else {
			if( $requirement ) {
				$_output .= '<span class="inputRequirement">' . $requirement . '</span>';
			} else {
				$_output .= '<span class="inputRequirement">*</span>';
			}
		}
	}
	
	return $_output;
}
//
?>
